package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.AccidentInfo;

import net.hn.hnms.biz.project.domain.ProjectInfo;
import net.hn.hnms.biz.project.mapper.AccidentInfoMapper;
import net.hn.hnms.biz.project.mapper.ProjectInfoMapper;
import net.hn.hnms.biz.project.request.AccidentInfoReq;

import net.hn.hnms.biz.project.service.AccidentInfoService;
import net.hn.hnms.biz.project.vo.AccidentInfoVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccidentInfoServiceImpl extends BaseService<AccidentInfo, AccidentInfoVo> implements AccidentInfoService {


    @Autowired
    private AccidentInfoMapper accidentInfoMapper;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;


    @Override
    public ResponsePage<AccidentInfoVo> selectPageList(AccidentInfoReq accidentInfoReq, PageQuery pageQuery) {
        //传项目ID
        Page<AccidentInfo> page = accidentInfoMapper.selectPage(pageQuery.build(), buildQuery(accidentInfoReq).orderByDesc(AccidentInfo::getCreateTime));
        List<AccidentInfoVo> list = page.getRecords().stream().map(o -> convert(o, AccidentInfoVo.class)).collect(Collectors.toList());

        for (AccidentInfoVo watchVO : list) {
            if (StrUtil.isNotBlank(watchVO.getProjectId())) {
                ProjectInfo projectInfo = projectInfoMapper.selectById(watchVO.getProjectId());
//                watchVO.setProjectType(projectInfo.getProjectType());
//                watchVO.setProjectDate(projectInfo.getProjectDate());
//                watchVO.setTotalAmount(projectInfo.getTotalAmount());
                if (projectInfo != null) {
                    watchVO.setProjectName(projectInfo.getProjectName());
                }
            }
        }

        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public int insert(AccidentInfoReq accidentInfoReq) {
        AccidentInfo insertObj = new AccidentInfo();
        BeanUtil.copyProperties(accidentInfoReq, insertObj);
        return accidentInfoMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        accidentInfoMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(AccidentInfoReq contractInfoReq) {
        AccidentInfo updateObj = new AccidentInfo();
        BeanUtil.copyProperties(contractInfoReq, updateObj);
        int updateCount = accidentInfoMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public AccidentInfo selectDetailById(String id) {
        AccidentInfo contractInfo = accidentInfoMapper.selectById(id);
        if (contractInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractInfo;
    }


    private LambdaQueryWrapper<AccidentInfo> buildQuery(AccidentInfoReq req) {
        return new LambdaQueryWrapper<AccidentInfo>()
                .like(StrUtil.isNotBlank(req.getAccidentPlace()), AccidentInfo::getAccidentPlace, req.getAccidentPlace())
                .eq(StrUtil.isNotBlank(req.getAccidentLevel()), AccidentInfo::getAccidentLevel, req.getAccidentLevel())
                .eq(StrUtil.isNotBlank(req.getAccidentType()), AccidentInfo::getAccidentType, req.getAccidentType())
                .eq(StrUtil.isNotBlank(req.getProjectId()), AccidentInfo::getProjectId, req.getProjectId())
                .eq(StrUtil.isNotBlank(req.getMineCode()), AccidentInfo::getMineCode, req.getMineCode());
    }
}
